package hotLeetcode;

import java.util.*;
import java.util.stream.Collectors;

public class Test202501 {

    //1 删除重复的字符串 保持原顺序不变
    public static List<String> removeDup(List<String> words) {
        //找一个数据结构， 不能包含重复的就行了
        return new ArrayList<>(new LinkedList<>(words));
    }


    //2. 给定一个Map<String,Integer>  找出年龄最大的人

    public static List<String> maxAge(Map<String, Integer> peoples) {
        //1. 用Steam 流
        //2. 遍历
        return peoples.entrySet().stream()
                .sorted((a, b) -> b.getValue() - a.getValue())
                .limit(3)
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
    }

    //3. 出现的次数
    public static Map<String, Integer> count(List<String> words) {
        HashMap<String, Integer> map = new HashMap<>();
        for (String word : words) {
            map.put(word, map.getOrDefault(word, 0) + 1);
        }

        return map;
    }

    //4.分离字母和数字
    public static Map<String, List<String>> separe(List<String> words) {
        List<String> numList = new ArrayList<>();
        List<String> wordsList = new ArrayList<>();
        for (String word : words) {
            if (word.matches("\\d+")) {
                numList.add(word);
            } else {
                wordsList.add(word);
            }
        }

        HashMap<String, List<String>> ans = new HashMap<>();
        ans.put("numbers", numList);
        ans.put("wordsList", wordsList);
        return ans;

    }

    // 5 计算平均值
    public static Map<String, String> avg(Map<String, List<Integer>> scores) {
        Map<String, String> ans = new HashMap<>();
        for (Map.Entry<String, List<Integer>> entry : scores.entrySet()) {
            List<Integer> list = entry.getValue();
            double avg = list.stream().mapToInt(Integer::intValue).average().orElse(0);
            ans.put(entry.getKey(), Double.toString(avg));

        }


        return ans;

    }


    //6. 解析邮件
    //k -> 名字 ， v -> 地址
    public static Map<String, String> pares(List<String> emails) {
        HashMap<String, String> map = new HashMap<>();
        for (String email : emails) {
            String[] split = email.split(",");
            if (split.length == 2) {
                map.put(split[1], split[0]);
            }

        }

        return map;
    }




}
